package com.jx.smartbot;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//import com.gocom.imoss.common.Exception.AuthorizedException;
import com.jx.smartbot.service.IRedisService;


@Configuration
class MvcConf extends WebMvcConfigurerAdapter {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MvcConf.class);
	public static final String JSON_UTF_8 = "application/json; charset=UTF-8";
	@Autowired
	IRedisService redisService;
	
	public void addInterceptors(InterceptorRegistry registry) {
	      registry.addInterceptor(new HandlerInterceptorAdapter() {

	          @Override
	          public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
	                                   Object handler) throws Exception {
	        	  String wptoken=request.getParameter("wptoken");
	        	  return isAuthory(wptoken,response);
	          }
	      }).addPathPatterns("/wps/*");
	  }
	private boolean isAuthory(String wptoken, HttpServletResponse response){
		boolean tag=false;
		if(wptoken.startsWith("wp_")){
			wptoken=wptoken.substring(3);
		}
		if(!redisService.exists(wptoken)){
			response.setStatus(HttpStatus.UNAUTHORIZED.value());
		}else{
			tag=true;
		}
		return tag;
	}
}
